package com.csun.cmny.provider.service.impl;

import com.csun.cmny.base.dto.LoginAuthDto;
import com.csun.cmny.base.enums.ErrorCodeEnum;
import com.csun.cmny.core.support.BaseService;
import com.csun.cmny.provider.dao.OmcCommentsMapper;
import com.csun.cmny.provider.dao.OmcServiceOrderMapper;
import com.csun.cmny.provider.exceptions.OmcBizException;
import com.csun.cmny.provider.model.constant.ServiceOrderConstant;
import com.csun.cmny.provider.model.domain.OmcComments;
import com.csun.cmny.provider.model.domain.OmcServiceOrder;
import com.csun.cmny.provider.model.dto.OmcServicedOrderDto;
import com.csun.cmny.provider.model.dto.ServiceCommentDto;
import com.csun.cmny.provider.model.dto.comments.CommentsQueryDto;
import com.csun.cmny.provider.model.dto.comments.CommentsSaveDto;
import com.csun.cmny.provider.model.vo.ServiceListCommentsVo;
import com.csun.cmny.provider.model.vo.ServicedOrderVo;
import com.csun.cmny.provider.model.vo.comments.CommentsVo;
import com.csun.cmny.provider.service.OmcCommentsService;
import com.csun.cmny.provider.service.OmcServiceOrderService;
import com.csun.cmny.util.PublicUtil;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestBody;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;


@Slf4j
@Service
@Transactional(rollbackFor = Exception.class)
public class OmcCommentsServiceImpl extends BaseService<OmcComments> implements OmcCommentsService {
    @Resource
    private OmcCommentsMapper commentsMapper;
    @Resource
    private OmcServiceOrderService serviceOrderService;
    @Resource
    private OmcServiceOrderMapper omcServiceOrderMapper;

    @Override
    public List<CommentsVo> selectComments(CommentsQueryDto selectComments) {
        return commentsMapper.selectComments(selectComments);
    }

    @Override
    public PageInfo listComments(ServiceCommentDto serviceCommentDto) {
        OmcServicedOrderDto omcServicedOrderDto=new OmcServicedOrderDto();
        //设置查询类型
        serviceCommentDto.setId((long) 0);
        BeanUtils.copyProperties(serviceCommentDto,omcServicedOrderDto);
        omcServicedOrderDto.setStatus(ServiceOrderConstant.HAVE_EVALUATION);
        List<ServicedOrderVo> voList = omcServiceOrderMapper.listServicedPage(omcServicedOrderDto);
        List<ServiceListCommentsVo> serviceListCommentsVoList=new ArrayList<>();
        for(ServicedOrderVo vo:voList){
            OmcComments omcComments=commentsMapper.selectByPrimaryKey(vo.getOrderId());
            if(PublicUtil.isEmpty(omcComments)) continue;
            ServiceListCommentsVo serviceListCommentsVo=new ServiceListCommentsVo();
            BeanUtils.copyProperties(vo,serviceListCommentsVo);
            serviceListCommentsVo.setOmcComments(omcComments);
            serviceListCommentsVoList.add(serviceListCommentsVo);
        }
        return new PageInfo<>(serviceListCommentsVoList);
    }

    @Override
    public int addComments(CommentsSaveDto commentsSaveDto, LoginAuthDto loginAuthDto) {

        /*校验订单是否存在*/
        OmcServiceOrder omcServiceOrder = serviceOrderService.selectByKey(commentsSaveDto.getServiceOrderId());
        if (PublicUtil.isEmpty(omcServiceOrder)){
            throw new OmcBizException(ErrorCodeEnum.OMC10032005);
        }
        if (omcServiceOrder.getOrderStatus()!=ServiceOrderConstant.ORDER_STATUS_COMPELETE){
            throw new OmcBizException(ErrorCodeEnum.OMC10032033);
        }
        //订单状态修改为已评论
        omcServiceOrder.setOrderStatus(ServiceOrderConstant.HAVE_EVALUATION);
        serviceOrderService.update(omcServiceOrder);


        /*校验是否评论过*/
        OmcComments comments = selectByKey(commentsSaveDto.getServiceOrderId());

        if(PublicUtil.isNotEmpty(comments)){
            throw new OmcBizException(ErrorCodeEnum.OMC10032034);
        }

        OmcComments omcComments = new OmcComments();

        BeanUtils.copyProperties(commentsSaveDto,omcComments);

        omcComments.setGroupId(loginAuthDto.getGroupId());
        omcComments.setUserId(loginAuthDto.getUserId());
        omcComments.setOperator(loginAuthDto.getUserName());
        return save(omcComments);
    }


}
